package m.jclecai.com.adapter.independentLotteryAdapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.MissDataBean;


/**
 * Created by lenovo on 2017/6/6.
 * <p>
 * 页面内的下拉历史开奖列表
 */

public class KSLotteryAdapter extends BaseAdapter {

    int[] images = new int[]{R.drawable.dice_one, R.drawable.dice_two, R.drawable.dice_three, R.drawable.dice_four, R.drawable.dice_five, R.drawable.dice_six};
    private final Context context;
    private List<MissDataBean.Codes> codes;

    public KSLotteryAdapter(Context context) {
        this.context = context;
    }


    public void setData(List<MissDataBean.Codes> codes) {
        this.codes = codes==null?new ArrayList<MissDataBean.Codes>():codes;
    }

    @Override
    public int getCount() {
        return codes.size();
    }

    @Override
    public Object getItem(int position) {
        return codes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.independent_lottery_item, null);
            holder.tv_term = (TextView) convertView.findViewById(R.id.tv_term);
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            holder.tv_form = (TextView) convertView.findViewById(R.id.tv_form);
            holder.tv_sum= ((TextView) convertView.findViewById(R.id.tv_sum));

            holder.iv_shazi_1 = (ImageView) convertView.findViewById(R.id.iv_shazi_1);
            holder.iv_shazi_2 = (ImageView) convertView.findViewById(R.id.iv_shazi_2);
            holder.iv_shazi_3 = (ImageView) convertView.findViewById(R.id.iv_shazi_3);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MissDataBean.Codes code = this.codes.get(position);
        String issue = code.getIssue();
        String substring = issue.substring(issue.length() - 2, issue.length());
        holder.tv_term.setText(substring+"期");
        holder.tv_number.setText(code.getCode().replace(","," "));
        holder.tv_form.setText(speciationAnalysis(code.getCode()));


        String[] split = code.getCode().split(",");
        holder.iv_shazi_1.setBackgroundResource(images[Integer.valueOf(split[0]) - 1]);
        holder.iv_shazi_2.setBackgroundResource(images[Integer.valueOf(split[1]) - 1]);
        holder.iv_shazi_3.setBackgroundResource(images[Integer.valueOf(split[2]) - 1]);

        holder.tv_sum.setText(Integer.valueOf(split[0])+Integer.valueOf(split[1])+Integer.valueOf(split[2])+"");

        return convertView;
    }

    String form = null;

    //根据CODE判断形态
    private String speciationAnalysis(String code) {
        String[] split = code.split(",");
        Integer one = Integer.valueOf(split[0]);
        Integer two = Integer.valueOf(split[1]);
        Integer three = Integer.valueOf(split[2]);

        if (one == two) {
            form = "二同号";
            if (two == three) {
                form = "三同号";
            }
        } else {
            if (two == three) {
                form = "二同号";
            }
        }

        if (one == two - 1 && two == three - 1) {
            form = " 三连号";
        } else if (one != two && two != three) {
            form = "三不同号";
        }


        return form;

    }

    private class ViewHolder {
        TextView tv_term;
        ImageView iv_shazi_1;
        ImageView iv_shazi_2;
        ImageView iv_shazi_3;
        TextView tv_number;
        TextView tv_form;
        TextView tv_sum;
    }
}
















